package org.balthie.demo.mat.histogram;

import java.io.IOException;
import java.util.Map;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.balthie.demo.opensource.apache.poi.SSXS.SXSSFUtils;
import org.balthie.demo.opensource.apache.poi.SSXS.XSSFUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AnalyseLauncher
{
    private static final Logger LOGGER = LoggerFactory.getLogger(AnalyseLauncher.class);
    
    static String packageTempletePath = "/excel/SXSSF/erpdata/sun&apacheTemplete.xlsx";
    
    static String targetPath = "/excel/SXSSF/erpdata/erpdata0811.xlsx";
    
    static String outputPath = "C:/Users/Administrator/Desktop/temp-package-compare.xlsx";
    
    public static void main(String[] args) throws IOException
    {
        GroupMapBuider.buildGroupMap(packageTempletePath);
        
        analysePackage(targetPath);
        SXSSFWorkbook wb = new SXSSFWorkbook();
        SXSSFSheet sheet = wb.createSheet("package anaylse");
        String packageName = null;
        Map<String, PackageMATInfo> result = GroupMapBuider.packageMap;
        LOGGER.debug("buildWorkbook begin list.size[{}] LastRowNum[{}]", result.size());
        for(int i = 0; i < result.size(); i++)
        {
            packageName = GroupMapBuider.sortedMapKey.get(i);
            int cellNum = 0;
            Row tempRow = sheet.createRow(i + 1);
            tempRow.createCell(cellNum++).setCellValue(String.valueOf(packageName));
            tempRow.createCell(cellNum++).setCellValue(String.valueOf(result.get(packageName).getClassCount()));
            tempRow.createCell(cellNum++).setCellValue(String.valueOf(result.get(packageName).getObjCount()));
            tempRow.createCell(cellNum++).setCellValue(String.valueOf(result.get(packageName).getSwallowHeap()));
        }
        SXSSFUtils.writeLocaFile(wb, outputPath);
    }
    
    private static void analysePackage(String path)
    {
        try (XSSFWorkbook newWB = XSSFUtils.readExcel(path))
        {
            XSSFRow row;
            XSSFCell cell;
            XSSFSheet newSheet = newWB.getSheetAt(0);
            
            Integer objectCount = null;
            Integer swallowHeap = null;
            for(int i = 1; i <= newSheet.getLastRowNum(); i++)
            {
                row = newSheet.getRow(i);
                if(row != null)
                {
                    cell = row.getCell(0);
                    if(cell != null)
                    {
                        for(String packagePrefix : GroupMapBuider.packageMap.keySet())
                        {
                            if(cell.getStringCellValue().startsWith(packagePrefix))
                            {
                                objectCount = (int) row.getCell(1).getNumericCellValue();
                                
                                swallowHeap = (int) row.getCell(2).getNumericCellValue();
                                
                                GroupMapBuider.packageMap.get(packagePrefix).plus(1, objectCount, swallowHeap);
                            }
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
